package com.jtbd.service;

import com.jtbd.constants.UserTypeEnum;
import com.jtbd.dto.http.UserEmployeeHttpDto;
import com.jtbd.dto.db.UserEmployeeInfo;
import com.jtbd.handler.UserLoginInfoHandler;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 学生基础业务操作相关的service
 */
@Service
public class EmployeeBaseOperateService {
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private UserEmployeeService userEmployeeService;
    @Autowired
    private UserLoginInfoHandler userLoginInfoHandler;

    public List<UserEmployeeInfo> employeeLogin(HttpServletResponse response, String phone, String password) {
        Long uid = userInfoService.userLogin(response, phone, UserTypeEnum.EMPLOYEE.getCode(), password);
        List<UserEmployeeInfo> userEmployeeInfos = userEmployeeService.getUserEmployeeInfoByUid(uid, false);
        if (!ObjectUtils.isEmpty(userEmployeeInfos)) {
            if (userEmployeeInfos.size() == 1) {
                userLoginInfoHandler.employeeLogin(userEmployeeInfos.get(0), response);
            }
            return userEmployeeInfos;
        }
        return null;
    }

    public UserEmployeeInfo employeeConfimLogin(HttpServletResponse response, Long eid) {
        UserEmployeeInfo userEmployeeInfo = userEmployeeService.getUserEmployeeInfoById(eid);
        if (!ObjectUtils.isEmpty(userEmployeeInfo)) {
            userLoginInfoHandler.employeeLogin(userEmployeeInfo, response);
            return userEmployeeInfo;
        }
        return null;
    }

    public int employeeRegister(UserEmployeeHttpDto info) {
        return userEmployeeService.addUserEmployee(UserEmployeeHttpDto.getUserEmployeeInfo(info));
    }
}
